# -*- coding: utf-8 -*-
from django.contrib import admin
from django.contrib.admin.options import ModelAdmin
from django import forms

from Scope.models import *
from Shopping.models import *
from Tags.forms import FormTag
from Tags import aplicar_tags, tags_para_objeto

#Admin Notícia
class CategoriaAdmin(admin.ModelAdmin):
    fieldsets = [
        ('Informações',{'fields':('nome','descricao')}),
    ]
    list_display = ['nome','descricao']

class VideoAdmin(admin.ModelAdmin):
    class Media:
        js = ('/js/tiny_mce/tiny_mce.js', '/js/textareas.js')
    fieldsets = [
        ('Principais', {'fields':(('publicar'),('titulo'),'subtitulo', ('conteudo'),)}),
        ('Mídia', {'fields':('url','imagem')}),
    ]
    list_display = ['titulo', 'cliques']
    list_filter = ['publicar']
    search_fields = ['titulo']
    save_on_top= True

class EventoAdmin(admin.TabularInline):
    model = DataEvento
    max_num = 1

class VideoNoticiaAdmin(admin.TabularInline):
    fieldsets = [
        ('Extras', {'fields': ('usarVideo', 'video', 'descricaovideo'), 'classes':['collapse']})
    ]
    model = VideoNoticia
    extra = 1

class BaseNoticiaAdmin(admin.ModelAdmin):
    exclude = ('datacriacao', 'autor')
    form = FormTag
    #Salva as tags
    def save_model(self, request, obj, form, change):
        super (BaseNoticiaAdmin, self).save_model(request, obj, form, change)
        aplicar_tags(obj, form.cleaned_data['tags'])
    #Salva o usuário que cadastrou a notícia
    def save_model(self, request, obj, form, change):
        if getattr(obj, 'added_by', None) is None:
            obj.autor = request.user
        obj.last_modified_by = request.user
        obj.save()
    class Media:
        js = ('/js/tiny_mce/tiny_mce.js', '/js/textareas.js')

class AdminNoticia(BaseNoticiaAdmin):
    def get_form(self, request, obj=None, **kwargs):
        self.exclude = ('autor', 'slugnoticia', 'datacriacao')
        if not request.user.is_superuser:
            self.exclude.append('manchete')
        return super (AdminNoticia, self).get_form(request, obj, **kwargs)

    list_display = ['titulo','publicar','manchete', 'nomeCategoria', 'datacriacao', 'cliques']
    list_filter = ['autor','nomeCategoria', 'manchete']
    search_fields = ['titulo']
    save_on_top= True
    ordering = ['-datacriacao']
    inlines = [EventoAdmin, VideoNoticiaAdmin]

class LinkInline(admin.TabularInline):
    model = LinkDownload
    extra = 3

class AdminDownload(admin.ModelAdmin):
    fieldsets = [
        ('Principais', {'fields':(('album', 'nomeCantor', 'ano'),)}),
        ('Sobre o álbum', {'fields':('conteudo', 'imagem')}),
    ]
    inlines = [LinkInline]
    list_display = ['nomeCantor', 'album', 'cliques']

class GeneroAdmin(admin.ModelAdmin):
    fieldsets = [
        ('Principais', {'fields':['nome']}),
    ]
admin.site.register(Categoria, CategoriaAdmin)
admin.site.register(Video, VideoAdmin)
admin.site.register(Noticia, AdminNoticia)
admin.site.register(Download)
admin.site.register(Cantor)
admin.site.register(Genero, GeneroAdmin)
admin.site.register(LinkDownload)
admin.site.register(Letra)